﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;
using System.Data;

namespace DreamBuilder.UI.UserControls
{
    public partial class AttachmentDownload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string attachmentID = Request.QueryString["AttachmentID"];
            string AttachmentDBHelperString =  Request.QueryString["AttachmentDBHelperString"];
            AttachmentDAL attachmentDAL = UContainer.Resolve<AttachmentDAL>();
            attachmentDAL.AttachmentDBHelperString = AttachmentDBHelperString;
            DataTable dt = attachmentDAL.GetAttachmentByID(attachmentID);

            string attachmentName = dt.Rows[0]["AttachmentName"].ToString();
            byte[] AttachmentContent = dt.Rows[0]["AttachmentContent"] as byte[];
            //判断类型是否为PDF或者为word，如果是这两种，调用预览的。
            if (CheckMIMEType.IsWinWord(dt.Rows[0]["DocumentType"].ToString())||
                CheckMIMEType.IsWinExcel(dt.Rows[0]["DocumentType"].ToString())||
                 CheckMIMEType.IsWinPPT(dt.Rows[0]["DocumentType"].ToString()) ||
                CheckMIMEType.IsAdobePDF(dt.Rows[0]["DocumentType"].ToString())
                )
            {
                string path = "~/WebDoc/WebDoc.aspx?AttachmentID={0}&PageNum={1}&AttachmentDBHelperString={2}";
                path = string.Format(path, attachmentID, 1,AttachmentDBHelperString);
                //Server.Transfer(path, false);
                Response.Redirect(path);
            }
            else if (CheckMIMEType.IsImage(dt.Rows[0]["DocumentType"].ToString()))
            {
                string path = "~/ImageCutAndPrint.aspx?mode=fromserver&DBHelperString={0}&AttachmentId={1}";
                path = string.Format(path,  AttachmentDBHelperString,attachmentID);
                //Server.Transfer(path, false);
                Response.Redirect(path);
            }
            if (AttachmentContent == null)
            {
                Response.StatusCode = 404;
            }
            else
            {
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(attachmentName, System.Text.Encoding.UTF8) + "\"");
                Response.BinaryWrite(AttachmentContent);
                //modified by 陈杨 2011年4月14号17:21:25 修改上传文本文件增加后缀错误
                Response.Flush();
                Response.End();
            }
        }

        public static class CheckMIMEType
        {
            /// <summary>
            /// 判断是否为word
            /// </summary>
            /// <param name="documenttype"></param>
            /// <returns></returns>
            public static bool IsWinWord(string documenttype)
            {
                if (documenttype == "doc" || documenttype=="docx")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            /// 判断是否为excel
            /// </summary>
            /// <param name="documenttype"></param>
            /// <returns></returns>
            public static bool IsWinExcel(string documenttype)
            {
                if (documenttype == "xls" || documenttype == "xlsx")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            /// 判断是否为ppt
            /// </summary>
            /// <param name="documenttype"></param>
            /// <returns></returns>
            public static bool IsWinPPT(string documenttype)
            {
                if (documenttype == "ppt" || documenttype == "pptx")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            /// 判断是否为PDF
            /// </summary>
            /// <param name="documenttype"></param>
            /// <returns></returns>
            public static bool IsAdobePDF(string documenttype)
            {
                if (documenttype == "pdf" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            /// <summary>
            /// 判断是否是图片
            /// </summary>
            /// <param name="documenttype"></param>
            /// <returns></returns>
            public static bool IsImage(string documenttype)
            {
                switch (documenttype)
                {
                    case "bmp":
                        return true;
                    case "jpeg":
                        return true;
                    case "jpg":
                        return true;
                    case "gif":
                        return true;
                    case "exif":
                        return true;
                    case "tiff":
                        return true;
                    case "png":
                        return true;
                    default:
                        return false;
                }
            }
        }
    }
}
